package com.lemon.boot.service.impl;

import com.lemon.boot.common.base.BaseServiceImpl;
import com.lemon.boot.common.consts.ApiConst;
import com.lemon.boot.dao.DeptUserDAO;
import com.lemon.boot.dto.DeptUserDTO;
import com.lemon.boot.po.DeptUserPO;
import com.lemon.boot.service.IDeptUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 部门和用户关联表 业务实现类
 *
 * @author Lemon
 * @since 2024-02-29
 */
@Log4j2
@Service
public class DeptUserServiceImpl extends BaseServiceImpl<DeptUserDAO, DeptUserPO, DeptUserDTO> implements IDeptUserService {
    @Override
    public List<Long> getUserIdByDeptIdList(List<Long> deptIdList) {
        //1.封装请求参数
        Map<String, Object> param = ApiConst.enableStatusAndUnableDelete();
        param.put("deptIdList", deptIdList);

        //2.查询并返回
        return this.baseMapper.getUserIdByDeptIdList(param);
    }

    @Override
    public List<Long> getChildUserIdByUserId(Long userId) {
        List<DeptUserPO> deptUserList = this.query().select("dept_id").eq("user_id", userId).list();
        if (CollectionUtils.isEmpty(deptUserList)) {
            return new ArrayList<>(0);
        }
        return this.getUserIdByDeptIdList(deptUserList.stream().map(DeptUserPO::getDeptId).collect(Collectors.toList()));
    }
}